import 'package:flutter/material.dart';
import 'package:flutter_mvvm_demo/page/home/controller/home_page.dart';
import 'package:flutter_mvvm_demo/page/me/me_page.dart';
import 'package:flutter_mvvm_demo/page/search/search_page.dart';
import 'package:flutter_mvvm_demo/page/shop/shop_page.dart';
import 'package:flutter_mvvm_demo/widget/icons/icons.dart';

class MainBottomBar extends StatefulWidget {
  const MainBottomBar({Key? key}) : super(key: key);

  @override
  _MainBottomBarState createState() => _MainBottomBarState();
}

class _MainBottomBarState extends State<MainBottomBar> {
  late final List<Widget> _contentTab = [
    const HomePage(),
    const ShopPage(),
    const SearchPage(),
    const MePage()
  ];
  int _currentPage = 0;
  late PageController  _pageController;

  final List<BottomNavigationBarItem> _bottomItems = [];

  @override
  void initState() {
    super.initState();
    // 初始化底部导航
    _pageController = PageController(initialPage: _currentPage);
    initBottomBar();
  }

  /// 底部导航栏的创建
  void initBottomBar() {
    _bottomItems.add(const BottomNavigationBarItem(
      icon: Icon(
        IconFont.homeOutline,
        color: Color(0xFF262626),
        size: 28.0,
      ),
      activeIcon: Icon(
        IconFont.home,
        color: Color(0xFFDFBE84),
        size: 28.0,
      ),
      label: '首页',
    ));
    _bottomItems.add(const BottomNavigationBarItem(
      icon: Icon(
        IconFont.mallOutline,
        color: Color(0xFF262626),
        size: 28.0,
      ),
      activeIcon: Icon(
        IconFont.mall,
        color: Color(0xFFDFBE84),
        size: 28.0,
      ),
      label: '商城',
    ));
    _bottomItems.add(const BottomNavigationBarItem(
      icon: Icon(
        IconFont.shoeOutline,
        color: Color(0xFF262626),
        size: 28.0,
      ),
      activeIcon: Icon(
        IconFont.shoe,
        color: Color(0xFFDFBE84),
        size: 28.0,
      ),
      label: '发现',
    ));
    _bottomItems.add(const BottomNavigationBarItem(
      icon: Icon(
        IconFont.personOutline,
        color: Color(0xFF262626),
        size: 28.0,
      ),
      activeIcon: Icon(
        IconFont.person,
        color: Color(0xFFDFBE84),
        size: 28.0,
      ),
      label: '我的',
    ));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PageView(
        children: _contentTab,
        controller: _pageController,
        physics: const NeverScrollableScrollPhysics(),
      ),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        currentIndex: _currentPage,
        items: _bottomItems,
        onTap: (index) {
          setState(() {
            _currentPage = index;
          });
          _pageController.jumpToPage(index);
        },
      ),
    );
  }
}
